package com.web.surf.sample.eshop.database.dao.services;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import com.web.surf.sample.eshop.database.dao.interfaces.IImagesDao;
import com.web.surf.sample.eshop.faces.core.AbstractBean;
import com.web.surf.sample.eshop.faces.managed.beans.ImageDB;

public class ImageDaoImpl extends AbstractDao implements IImagesDao{
	
	private static final String SQL_IMAGE_BYID = "SELECT * FROM images WHERE image_id=?";
	private static final String SQL_SET_IMAGE = "INSERT INTO images VALUES(?,?,?)";
	private static final String SQL_IMAGE_BYGOODSID = "SELECT * FROM images WHERE image_id=?";
	private static final String PATH = "D:"+File.separator+"magazin"+File.separator+"images";

	public ImageDB getImageByID(String imageID) {
		try {
			PreparedStatement getImage = getConnection().prepareStatement(SQL_IMAGE_BYID);
			getImage.setString(1, imageID);
			return executeImageSelect(getImage);
		} catch (SQLException e) {
			terminateConnection();
			return null;
		}
	}
	
	public ImageDB getImageByGoodsID(int goodsID) {
		try {
			PreparedStatement getImage = getConnection().prepareStatement(SQL_IMAGE_BYGOODSID);
			getImage.setInt(1, goodsID);
			return executeImageSelect(getImage);
		} catch (SQLException e) {
			terminateConnection();
			return null;
		}
	}
	@Deprecated
	public void setImageIntoDB(String fileName) {
		try {
		getConnection().setAutoCommit(false);
			String filePath = PATH;
			File imageFile = new File(filePath, fileName);	
			FileInputStream inputStream = new FileInputStream(imageFile);
			PreparedStatement setImage = getConnection().prepareStatement(SQL_SET_IMAGE);
				setImage.setString(1, UUID.randomUUID().toString());
				setImage.setString(2, imageFile.getName());
				setImage.setBinaryStream(3, inputStream, (int)imageFile.length());
				setImage.executeUpdate();
			getConnection().commit();	
		} catch (FileNotFoundException e) {	
			e.printStackTrace();
			 }
		 catch (SQLException e) {		
			terminateConnection();			
		 }
		 finally {
			 try {
				getConnection().setAutoCommit(true);				
			} catch (SQLException e) {
				e.printStackTrace();
			}
			 
		 }
		}

	public void setImageIntoDB(String fileName, String imageID) {
		try {
			getConnection().setAutoCommit(false);
			String filePath = PATH;			
			File imageFile = new File(filePath, fileName);	
			FileInputStream inputStream = new FileInputStream(imageFile);
			PreparedStatement setImage = getConnection().prepareStatement(SQL_SET_IMAGE);
				setImage.setString(1, imageID);
				setImage.setString(2, imageFile.getName());
				setImage.setBinaryStream(3, inputStream, (int)imageFile.length());
				setImage.executeUpdate();
			getConnection().commit();	
		} catch (FileNotFoundException e) {	
			e.printStackTrace();
			 }
		 catch (SQLException e) {		
			terminateConnection();			
		 }
		 finally {
			 try {
				getConnection().setAutoCommit(true);				
			} catch (SQLException e) {
				e.printStackTrace();
			}			 
		 }
		}

	
	protected Map<?, ?> processMapSelect(ResultSet rs) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	
	protected int processSelectCount(ResultSet rs) throws SQLException {
		// TODO Auto-generated method stub
		return 0;
	}

	
	protected List<AbstractBean> processSelectResult(ResultSet rs)
			throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}	
}
